/**
 * Created by liuy on 2016/9/26.
 */

var statistics = function () {

    //校验字段
    var checkValue = function () {
        var validateMessageBox = $('#validateMessageBox');
        var configEditForm = $('#configEditForm');
        configEditForm.validate({
            errorElement: 'label',
            errorClass: 'help-block',
            focusInvalid: true,
            rules: {
                livelyTime: {
                    required: true,
                    max: 720,
                    min: 24,
                    digits: true
                },
                recordNumber: {
                    required: true,
                    max: 1000000,
                    min: 1,
                    digits: true
                },
                recordAlarm: {
                    required: true,
                    min: 1,
                    digits: true
                }
            },
            messages: {
                livelyTime: {
                    required: "平台设备活跃值为必填字段!",
                    max: "平台设备活跃值最大不能超过720小时！",
                    min: "平台设备活跃值最小不能低于24小时！",
                    digits: "平台设备活跃值必须是纯数字！"
                },
                recordNumber: {
                    required: "审计迹最大值为必填字段!",
                    max: "审计迹最大值不能超过1百万！",
                    min: "审计迹最大值不能低于1！",
                    digits: "审计迹最大值必须是纯数字！"
                },
                recordAlarm: {
                    required: "审计迹提醒值为必填字段!",
                    min: "审计迹提醒值不能低于1！",
                    digits: "审计迹提醒值必须是纯数字！"
                },
            },
            highlight: function (element) {
                $(element).closest('.check-class').removeClass('has-success').addClass('has-error');
            },
            unhighlight: function (element) {
                $(element).closest('.check-class').removeClass('has-error').addClass('has-success');
            },
            success: function (label, element) {
                label.closest('.check-class').removeClass('has-error');
            },
            submitHandler: function (form) {
                App.blockUI(window, true);
                form.submit();
            },
            errorContainer: validateMessageBox,
            errorPlacement: function (error, element) {
                this.errorContainer.addClass('note-danger').html("<p>输入有误，请先更正。</p>").show();
                var formGroup = $(element).closest('.check-class');
                var errorElement = formGroup.children('span.' + this.errorClass);
                if (!errorElement || errorElement.length == 0) {
                    errorElement = $('<span/>').addClass(this.errorClass);
                    errorElement.appendTo(formGroup);
                }
                error.appendTo(errorElement);
            }
        });
    }

    //删除时间段
    var deleteTime = function () {
        $('.btn.btn-xs.red-stripe').each(function (index, element) {
            $(element).on("click", function () {
                $(element).parent().remove();
                if ($("#md").children().length < 5) {
                    $("#addBtn,.btn-logo").show();
                }
                if ($("#md").children().length == 1) {
                    $(".red-stripe").hide();
                }
                //重铸字段排序
                $("#md > div").each(function (indexDiv, elementDiv) {
                    $("#" + elementDiv.id + " > input").each(function (indexInput, elementInput) {
                        if (indexInput == 0) {
                            $(elementInput).attr("name", 'statisticsTimes[' + indexDiv + '].name');
                        }
                        if (indexInput == 1) {
                            $(elementInput).attr("name", 'statisticsTimes[' + indexDiv + '].beginTime');
                        }
                        if (indexInput == 2) {
                            $(elementInput).attr("name", 'statisticsTimes[' + indexDiv + '].endTime');
                        }
                        if (indexInput == 3) {
                            $(elementInput).attr("id", 'deleteTime' + indexDiv);
                        }
                    })
                    $(elementDiv).attr("id", "div" + indexDiv);
                });
            });
        });
    };

    var addTime = function () {
        $("#addBtn,.btn-logo").click(function () {
            var i = "<div class='form-group form-group-input' id='div" + $("#md").children().length + "'> <input type='text'  class='form-input-c placeholder-color' name='statisticsTimes[" + $("#md").children().length + "].name' onfocus='inputPlaceholderIn(this)' value='请输入时间段名称' maxlength='20' onblur='namePlaceholderOut(this)'/> " +
                "&nbsp &nbsp 区间值（始）&nbsp &nbsp <input class='form-input-c placeholder-color'  type='text'  onfocus='inputPlaceholderIn(this)' onblur='startPlaceholderOut(this)'  name='statisticsTimes[" + $("#md").children().length + "].beginTime' value='起始时间' maxlength='5' />" +
                "&nbsp &nbsp 区间值（末）&nbsp &nbsp <input class='form-input-c placeholder-color' type='text'  onfocus='inputPlaceholderIn(this)' onblur='endPlaceholderOut(this)'  name='statisticsTimes[" + $("#md").children().length + "].endTime' value='结束时间' maxlength='5' /> &nbsp" +
                " <a class='btn btn-xs red-stripe' shiro:has-permission='sys:statistics:edit' id='deleteTime" + $("#md").children().length + "'>删除</a><br/></div>"

            $("#md").append(i);
            deleteTime();
            if ($("#md").children().length > 1) {
                $(".red-stripe").show();
            }
            if ($("#md").children().length == 5) {
                $("#addBtn,.btn-logo").hide();
            }
        });

        if ($("#md").children().length == 1) {
            $(".red-stripe").hide();
        }

        //隐藏添加按钮
        if ($("#md").children().length == 5) {
            $("#addBtn,.btn-logo").hide();
        }
    };

    //记录能够修改参数值
    var changeInputTrue = function () {
        $("#make").on("click", function () {
            $("[name=recordAlarm]").attr("readonly", false);
            $("[name=recordNumber]").attr("readonly", false);
        })
    };

    //不记录不能够修改参数值
    var changeInputFalse = function () {
        $("#noMake").on("click", function () {
            $("[name=recordAlarm]").attr("readonly", true);
            $("[name=recordNumber]").attr("readonly", true);
        })
    };

    return {
        //初始化
        init: function () {
            checkValue();
            deleteTime();
            addTime();
            changeInputTrue();
            changeInputFalse();
        }
    }

}();
